//
//  CompostViewController.m
//  OrderSeat
//
//  Created by 王威 on 14-1-6.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "ComposeViewController.h"

#define INPUT_MARK @"填写评论"

@interface ComposeViewController ()

@property (strong, nonatomic) UIColor *oldColor;

@end

@implementation ComposeViewController

- (id)initWithId:(NSString *)pid forType:(CommentsType)type
{
    self = [super initWithNibClass:[ComposeViewController class]];
    if (self) {
        _pid  = pid;
        _type = type;
        _dataSource = [[ComposeDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

#pragma 隐藏tabbar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"点评"];
    
    _commentsTV.delegate = self;
    [_commentsTV drawBorder];
    _oldColor = _commentsTV.textColor;
    _submitBtn.top = _commentsTV.bottom_10;
    
    [self.view addTarget:self action:@selector(viewTapped:)];
}

- (void)leftButtonClick:(id)sender
{
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)submitAction:(id)sender
{
    if ([_commentsTV.text isEqualToString:S_EMPTY]) {
        [AppDelegate showMessage:@"点评内容不能为空！"];
        return;
    }
    
    [SVProgressHUD show];
    [_commentsTV resignFirstResponder];
    [_dataSource composeWithText:_commentsTV.text andType:_type];
}

-(void)viewTapped:(UITapGestureRecognizer*)tapGr
{
    if ([_commentsTV resignFirstResponder]) {
        if ([_commentsTV.text isEqualToString:@""]) {
            _commentsTV.text = INPUT_MARK;
            _commentsTV.textColor = _oldColor;
        }
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:INPUT_MARK]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([_commentsTV.text isEqualToString:@""]) {
        _commentsTV.text = INPUT_MARK;
        _commentsTV.textColor = _oldColor;
    }
}

@end

//===========================================================================================
#pragma ===================数据源、数据请求等封装================================================
//===========================================================================================

@implementation ComposeDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if (self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)composeWithText:(NSString *)text andType:(CommentsType)type
{
    NSString *api = S_EMPTY;
    switch (type) {
        case Coupon: api = API_COUPON_COMPOSE; break;
        case Gourmet: api = API_GOURMET_COMPOSE; break;
        case Activity: api = API_ACTIVITY_COMPOSE; break;
        case Photo: api = API_PHOTO_COMPOSE; break;
        default: break;
    }
    
    NSString *url = [NSString stringWithFormat:api,_viewController.pid,text,[AppSetting userName]];
    
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];
    
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        [SVProgressHUD dismissWithError:@"服务器错误，点评失败！"];
        return;
    }
    
    NSDictionary *data = [json objectForKey:@"data"];
    if ([[data objectForKey:@"state"] intValue] == 1) {
        [SVProgressHUD dismissWithSuccess:@"点评成功！"];
        NSLog(@"%@",[_viewController.parentViewController class]);
        _viewController.parentVC.isBack = YES;
        [_viewController performSelector:@selector(leftButtonClick:) withObject:nil afterDelay:1];
    }
}

@end


